//
//  BaseController.m
//  deerkids
//
//  Created by 王昭的Mac Air on 2017/8/28.
//  Copyright © 2017年 wangzhao. All rights reserved.
//

#import "BaseController.h"

#import "MyButton.h"
#import "UIImage+ImageWithColor.h"
#import "AppDelegate.h"

@interface BaseController ()

@end

@implementation BaseController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    
    
    ((AppDelegate*)[UIApplication sharedApplication].delegate).pageAction = @"";
    
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

-(void)buttonClick:(UIButton *) sender{
    if(sender.tag == BACK_BUTTON){
        [self backAction];
    }
}


-(void)createTitleMenu{
    self.titleMenu=[[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, NavBarHeight)];
    [self.titleMenu setBackgroundColor:UIColorFromRGB(BgTitleColor)];
    [self.titleMenu setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [self.view addSubview:self.titleMenu];
    
    
    self.menuTitleButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.menuTitleButton setFrame:CGRectMake(60, StatusBarHeight, ScreenWidth-120, 44)];
    [self.menuTitleButton setBackgroundColor:[UIColor clearColor]];
    [self.menuTitleButton.titleLabel setFont:TitleFont];
    [self.menuTitleButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.menuTitleButton setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [self.titleMenu addSubview:self.menuTitleButton];
    
    
    self.menuLeftButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.menuLeftButton setFrame:CGRectMake(10, StatusBarHeight, 60, 44)];
    self.menuLeftButton.tag=BACK_BUTTON;
    [self.menuLeftButton.titleLabel setFont:ListTitleFont];
    [self.menuLeftButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.menuLeftButton setTitleColor:UIColorFromRGBAlpha(TextWhiteColor, 0.7) forState:UIControlStateHighlighted];
    [self.menuLeftButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuLeftButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.menuLeftButton setImage:[UIImage imageNamed:@"login_close"] forState:UIControlStateNormal];
    [self.menuLeftButton setImage:[UIImage imageNamed:@"login_close"] forState:UIControlStateHighlighted];
    [self.menuLeftButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin];
    [self.menuLeftButton setTitle:@"   " forState:UIControlStateNormal];
    [self.titleMenu addSubview:self.menuLeftButton];
    
    self.menuRightButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.menuRightButton setFrame:CGRectMake(ScreenWidth-44, StatusBarHeight, 44, 44)];
    self.menuRightButton.tag=RIGHT_BUTTON;
    [self.menuRightButton.titleLabel setFont:ListTitleFont];
    [self.menuRightButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.menuRightButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.menuRightButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.menuRightButton setImage:[UIImage imageNamed:@"setting_nor"] forState:UIControlStateNormal];
    [self.menuRightButton setImage:[UIImage imageNamed:@"setting_sel"] forState:UIControlStateHighlighted];
    [self.menuRightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.menuRightButton setTitleColor:UIColorFromRGBAlpha(TextWhiteColor, 0.7) forState:UIControlStateHighlighted];
    [self.menuRightButton setTitleColor:UIColorFromRGBAlpha(TextWhiteColor, 0.7) forState:UIControlStateDisabled];
    [self.menuRightButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [self.titleMenu addSubview:self.menuRightButton];
    
    self.otherButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [self.otherButton setFrame:CGRectMake(self.view.frame.size.width-88, StatusBarHeight , 44, 44)];
    self.otherButton.tag=OTHER_BUTTON;
    [self.otherButton.titleLabel setFont:ListTitleFont];
    [self.otherButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.otherButton.imageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.otherButton setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];
    [self.otherButton setImage:[UIImage imageNamed:@"setting_nor"] forState:UIControlStateNormal];
    [self.otherButton setImage:[UIImage imageNamed:@"setting_sel"] forState:UIControlStateHighlighted];
    [self.otherButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
    [self.titleMenu addSubview:self.otherButton];
    self.otherButton.hidden=YES;
}


#pragma mark - set get
- (void)setNavigationBar
{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back_title"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}


-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


-(void)openPushVC:(UIViewController *) vc{
    [self.navigationController pushViewController:vc animated:YES];
}

// 支持设备自动旋转
- (BOOL)shouldAutorotate
{
    return YES;
}
// 支持横竖屏显示
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskPortrait;
}

-(UIView *)createTips:(NSString *)tip y:(CGFloat) yy{
    UIView *tipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0)];
    [tipView setBackgroundColor:[UIColor clearColor]];
    
    UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12, 2, 15)];
//    [iconView setImage:[UIImage imageNamed:@"icon_tag_temp"]];
    [iconView setBackgroundColor:BASE_COLOR];
    [iconView setContentMode:UIViewContentModeScaleAspectFit];
    iconView.tag = 1;
    [tipView addSubview:iconView];
    
    
    UILabel *lblTips = [[UILabel alloc] init];
    [lblTips setFont:[UIFont boldSystemFontOfSize:k12FontSize]];
    [lblTips setTextColor:DEEP_GRAY_COLOR];
    [lblTips setText:tip];
    [lblTips setNumberOfLines:0];
    [lblTips setBackgroundColor:[UIColor clearColor]];
    [tipView addSubview:lblTips];
    
    CGSize maximumLabelSize = CGSizeMake(SCREEN_WIDTH, MAXFLOAT);
    CGSize expectedLabelSize = [lblTips sizeThatFits:maximumLabelSize];
    
    CGFloat height = expectedLabelSize.height;
    if(height<20){
        height = 20;
    }
    [lblTips setFrame:CGRectMake(30, 9, SCREEN_WIDTH-45, height)];
    
    [tipView setFrame:CGRectMake(0, 0, SCREEN_WIDTH, height + 12)];
    
    return tipView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)createPlaceholderView:(NSString *)title message:(NSString *)message image:(UIImage *)image withView:(UIView *)superView action:(void (^)(UIButton *button)) clickblock{
    if (_placeholderView) {
        [_placeholderView removeFromSuperview];
        _placeholderView = nil;
    }
    if(superView==nil){
        superView=self.view;
    }
    
    _placeholderView = [[UIView alloc]initWithFrame:superView.bounds];
    
    
    [_placeholderView setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin];
    
    [_placeholderView setAutoresizesSubviews:YES];
    [_placeholderView setBackgroundColor:BG_GRAY_COLOR];
    //    [_placeholderView setBackgroundColor:UIColorFromRGB(BgSystemColor)];
    [superView addSubview:_placeholderView];
    CGFloat y = superView.frame.size.height / 2 - 80;
    CGRect pf = CGRectMake(0, y, superView.bounds.size.width, 0);
    if(image){
        UIImageView *icon = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"network_fail"]];
        
        [icon setImage:image];
        
        [icon setContentMode:UIViewContentModeCenter];
        [icon setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        icon.frame = CGRectMake(0,pf.origin.y, pf.size.width, image.size.height);
        [_placeholderView addSubview:icon];
        y= y + icon.frame.size.height+10;
    }
    if(title){
        UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, y, pf.size.width, 30)];
        [lblTitle setText:title];
        [lblTitle setFont:[UIFont systemFontOfSize:k12FontSize]];
        [lblTitle setTextColor:DEEP_GRAY_COLOR];
        [lblTitle setTextAlignment:NSTextAlignmentCenter];
        [lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [lblTitle setNumberOfLines:0];
        [lblTitle setBackgroundColor:[UIColor clearColor]];
        [_placeholderView addSubview:lblTitle];
        y=y+30+5;
    }
    
    if(message){
        UILabel *lblTitle=[[UILabel alloc] initWithFrame:CGRectMake(0, y, pf.size.width, 30)];
        [lblTitle setText:message];
        [lblTitle setFont:[UIFont systemFontOfSize:k12FontSize]];
        [lblTitle setTextColor:LIGHT_GRAY_COLOR];
        [lblTitle setTextAlignment:NSTextAlignmentCenter];
        [lblTitle setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
        [lblTitle setAutoresizesSubviews:YES];
        [lblTitle setBackgroundColor:[UIColor clearColor]];
        [_placeholderView addSubview:lblTitle];
        y=y+30;
    }
    
    // 如果有重新加载方法，就显示重新加载按钮
    if(clickblock){
        MyButton *reButton=[MyButton buttonWithType:UIButtonTypeCustom];
        [reButton setFrame:CGRectMake(pf.size.width/2-102/2, y+5, 102, 32)];
        [reButton setBackgroundImage:[UIImage imageWithColor:BASE_COLOR] forState:UIControlStateNormal];
        [reButton setBackgroundImage:[UIImage imageWithColor:BASE_COLOR] forState:UIControlStateDisabled];
        [reButton setTitle:@"点击重试" forState:UIControlStateNormal];
        [reButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [reButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        [reButton.titleLabel setFont:[UIFont systemFontOfSize:k12FontSize]];
        reButton.objTag = clickblock;
        [reButton addTarget:self action:@selector(reloadButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [reButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin];
        reButton.layer.cornerRadius  = 16;
        reButton.layer.masksToBounds = YES;
        [_placeholderView addSubview:reButton];
        
        
        UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        activityIndicator.tag=10;
        [activityIndicator setFrame:CGRectMake(192/2-45, 15, 20, 20)];
        [reButton addSubview:activityIndicator];
        
        y=y+60;
    }
}

-(void)reloadButtonClick:(MyButton *)btn{
    [btn setTitle:@"      加载中" forState:UIControlStateNormal];
    // 变灰
    //    [btn setBackgroundImage:[UIImage imageWithColor:UIColorFromRGB(BgVCodeColor)] forState:UIControlStateDisabled];
    
    UIActivityIndicatorView *activityIndicator = [btn viewWithTag:10];
    [activityIndicator startAnimating];
    ((void (^)(UIButton *button))btn.objTag)(btn);
}


- (void)removePlaceholderView{
    if (_placeholderView && _placeholderView!=nil) {
        [_placeholderView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [_placeholderView removeFromSuperview];
        
        _placeholderView = nil;
    }
}







-(UIView *)createTableFooter{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 44)];
    UILabel *label = [self createLabel];
    [label setFrame:headerView.bounds];
    [label setTextColor:UIColorFromRGB(Text6Color)];
    [label setFont:FontThirteen];
    [label setText:Uncontent_floor];
    [label setTextAlignment:NSTextAlignmentCenter];
    [headerView addSubview:label];
    
    return headerView;
}

-(UIImageView *)createImageView{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;
    return imgView;
}

-(UILabel *) createLabel{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [label setTextAlignment:NSTextAlignmentLeft];
    [label setFont:FontThirteen];
    [label setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [label setBackgroundColor:UIColor.clearColor];
    [label setNumberOfLines:0];
    return label;
}

-(UIButton *) createButton{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn.titleLabel setFont:FontFiftent];
    return btn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
